package com.atguigu.tingshu.user.strategy;

import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.user.strategy.impl.AlbumStrategy;
import com.atguigu.tingshu.user.strategy.impl.TrackStrategy;
import com.atguigu.tingshu.user.strategy.impl.VIPStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author: atguigu
 * @create: 2023-12-02 09:24
 */
@Slf4j
@Component
public class StrategyFactory {

    /**
     * 将IOC容器中PaidRecordStrategy接口类型对象注入Map中
     * Map中key是bean对象ID Map中Value是实现类对象
     */
    @Autowired
    private Map<String, PaidRecordStrategy> strategyMap;

    /**
     * 根据入参中类型自动返回策略实现类对象
     *
     * @param itemType
     * @return
     */
    public PaidRecordStrategy getPaidStrategy(String itemType) {
        if (strategyMap.containsKey(itemType)) {
            return strategyMap.get(itemType);
        }
        throw new GuiguException(ResultCodeEnum.ARGUMENT_VALID_ERROR);
    }
}
